package com.wx.service.impl;

import com.wx.constants.Constants;
import com.wx.constants.RedisKeyPrefix;
import com.wx.entity.*;
import com.wx.exception.BusinessException;
import com.wx.mapper.SysHospitalMapper;
import com.wx.service.*;
import com.wx.utils.SnowFlakeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SysHospitalServiceImpl extends BaseServiceImpl<SysHospital> implements SysHospitalService {

    @Resource
    private SysHospitalMapper sysHospitalMapper;

    @Resource
    private SysGeneratorCodeService sysGeneratorCodeService;

    @Resource
    private SysRangeNurseService sysRangeNurseService;

    @Resource
    private SysAddressService sysAddressService;

    @Resource
    private SysRangeHospitalService sysRangeHospitalService;

    @Resource
    private SysNurseService sysNurseService;


    @Override
    public int saveHospital(SysHospital hospital) {
        String uuid = sysGeneratorCodeService.fetchUUIDWithPreFix(RedisKeyPrefix.HOSPITAL_KEY, "HOS",10, false);
        hospital.setId(SnowFlakeUtil.getStringId());
        hospital.setHospitalCode(uuid);
        return sysHospitalMapper.insertSelective(hospital);
    }

    @Override
    public List<SysHospital> listHospitalByNurseId(String openId) {
        SysNurse sysNurse = new SysNurse();
        sysNurse.setOpenId(openId);
        sysNurse.setDelFlag(Constants.DELETE_NO);
        SysNurse nurse = sysNurseService.selectOne(sysNurse);
        if(nurse == null){
            throw new BusinessException("护士信息不存在");
        }
        SysRangeNurse sysRangeNurse = sysRangeNurseService.selectByNurseId(nurse.getId());
        if(sysRangeNurse == null){
            throw new BusinessException("请联系后台管理员给您绑定对应的所属范围");
        }
        return sysHospitalMapper.listHospitalByRangeId(sysRangeNurse.getRangeId());
    }

    @Override
    public List<SysHospital> listPage(SysHospital param) {
        List<SysHospital> sysHospitals = sysHospitalMapper.listPage(param);
        if(!CollectionUtils.isEmpty(sysHospitals)){
            List<SysAddress> sysAddresses = sysAddressService.getAll();
            Map<String, String> addRessMap = sysAddresses.stream().collect(Collectors.toMap(SysAddress::getCode, SysAddress::getName));
            List<String> rangeIds = sysHospitals.stream().map(SysHospital::getRangeId).collect(Collectors.toList());
            List<SysRangeNurse> rangeNurses =  sysRangeNurseService.selectByRangeIds(rangeIds);
            Map<String, List<SysRangeNurse>> rangeNurseMap = rangeNurses.stream().collect(Collectors.groupingBy(SysRangeNurse::getRangeId));
            for (SysHospital hospital : sysHospitals) {
                hospital.setProvinceName(addRessMap.get(hospital.getProvinceCode()));
                hospital.setCityName(addRessMap.get(hospital.getCityCode()));
                hospital.setAreaName(addRessMap.get(hospital.getAreaCode()));
                // 设置所属护士信息
                List<SysRangeNurse> nurseList = rangeNurseMap.get(hospital.getRangeId());
                if(!CollectionUtils.isEmpty(nurseList)){
                    String nurseName = nurseList.stream().filter(f -> !StringUtils.isEmpty(f.getNurseName())).map(SysRangeNurse::getNurseName).reduce((x, y) -> x + "," + y).get();
                    hospital.setNurseName(nurseName);
                    String nurseId = nurseList.stream().filter(f -> !StringUtils.isEmpty(f.getNurseId())).map(SysRangeNurse::getNurseId).reduce((x, y) -> x + "," + y).get();
                    hospital.setNurseId(nurseId);
                }
            }
        }
        return sysHospitals;
    }

    @Override
    public int deleteById(SysHospital hospital) {
        SysRangeHospital sysRangeHospital = sysRangeHospitalService.selectByHospitalId(hospital.getId());
        if(sysRangeHospital != null){
            throw new BusinessException("该机构还与范围存在绑定关系，不能进行删除");
        }

        hospital.setDelFlag(Constants.DELETE_YES);
        return sysHospitalMapper.updateByPrimaryKeySelective(hospital);
    }

    @Override
    public List<SysHospital> listHospital() {
        return sysHospitalMapper.listHospital();
    }

    @Override
    public List<SysHospital> listByProvinceCode(SysHospital sysHospital) {
        if(StringUtils.isEmpty(sysHospital.getProvinceCode())){
            throw new BusinessException("省份编码不能为空");
        }
        if(StringUtils.isEmpty(sysHospital.getCityCode())){
            throw new BusinessException("城市编码不能为空");
        }
        return sysHospitalMapper.listByProvinceCode(sysHospital);
    }

    @Override
    public List<SysHospital> getAll(SysHospital sysHospital) {
        return sysHospitalMapper.getAll(sysHospital);
    }

    @Override
    public List<SysHospital> selectByIds(List<String> hospitalIds) {
        return sysHospitalMapper.selectByIds(hospitalIds);
    }

    @Override
    public List<SysHospital> selectAllDetail() {
        return sysHospitalMapper.selectAllDetail();
    }
}